/*
 * Created on 05-Sep-2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package wstrader.task.quality;

import java.util.Vector;

import pc.sql.value.DateValue;
import pc.sql.value.FloatValue;
import pc.sql.value.LongValue;
import pc.sql.value.StringValue;
import pc.system.Tasks;
import pc.util.Utils;

/**
 * @author NANSA
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SampleShipDelvy extends SampleTask
{
	  public SampleShipDelvy(Tasks owner)
	  {
	    super(owner,"sampleshipdelvy");
	  }
	  
	  public long doTask()
	  {
	    int task_id     = this.getRequest().getIntValue("task_id");
	    long focused_id = this.getRequest().getLongValue("sample_details");
	    if (focused_id == 0) focused_id = this.getRequest().getLongValue("sample_master");
	    
	    int RecId = this.getRequest().getIntValue("rec_id");
	    
	    switch (task_id)
		{
	      case 1 : // Update
	      	if (RecId > 0)
	      	{
	      		focused_id = doUpdate("sample_details",focused_id);
	      		int ret = updateQualityReport(focused_id,"SD");
	      	}
	        break;
	      case 2 : // Delete

	        break;
	      default : //View only
	        break;
	    }

	    return focused_id;
	  }	 
	  
	  public int updateQualityReport(long SamDtsId,String Type)
	  {
	  	int ret = 0;
	  	String Sql ="";
	  	String StrQDt = Utils.dateToStr(Utils.today());
	  	String QRef = this.GenRefNumber("QR WC",StrQDt.substring(7),"quality_report");
	  	float Sc20_abv = getRequest().getFloatValue("above_sc20");
	  	float Sc19 = getRequest().getFloatValue("sc19");
	  	float Sc18 = getRequest().getFloatValue("sc18");
	  	float Sc17 = getRequest().getFloatValue("sc17");
	  	float Sc16 = getRequest().getFloatValue("sc16");
	  	float Sc15 = getRequest().getFloatValue("sc15");
	  	float Sc14 = getRequest().getFloatValue("sc14");
	  	float Sc13 = getRequest().getFloatValue("sc13");
	  	float Sc12 = getRequest().getFloatValue("sc12");
	  	float Sc12_blw = getRequest().getFloatValue("below_sc12");
	  	float Black = getRequest().getFloatValue("black");
	  	float Brown = getRequest().getFloatValue("brown");
	  	float FM = getRequest().getFloatValue("foreign_matter");
	  	float Broken = getRequest().getFloatValue("broken");
	  	
	  	long RecExist = this.findID("quality_report","sample_dts_id",SamDtsId);
	  	System.out.println("RectExit:-"+RecExist);
	  	if (RecExist <= 0)
	  	{
	  		Sql = "insert quality_report " +
  			"SET type = ?, " +
  			"ref_number = ?, " +
  			"date = ?, " +
  			"sc20 = ?, " +
  			"sc19 = ?, " +
  			"sc18 = ?, " +
  			"sc17 = ?, " +
  			"sc16 = ?, " +
  			"sc15 = ?, " +
  			"sc14 = ?, " +
  			"sc13 = ?, " +
			"sc12 = ?, " +
			"below_sc12 = ?, " +
			"black = ?, " +
			"brown = ?, " +
			"foreign_matter = ?, " +
			"broken = ?, " +
			"sample_dts_id =?";
	  		
    		Vector vp = new Vector();
    		vp.addElement(new StringValue(Type));
    		vp.addElement(new StringValue(QRef));
    		vp.addElement(new DateValue(Utils.today()));
    		vp.addElement(new FloatValue(Sc20_abv));
    		vp.addElement(new FloatValue(Sc19));
    		vp.addElement(new FloatValue(Sc18));
    		vp.addElement(new FloatValue(Sc17));
    		vp.addElement(new FloatValue(Sc16));
    		vp.addElement(new FloatValue(Sc15));
    		vp.addElement(new FloatValue(Sc14));
    		vp.addElement(new FloatValue(Sc13));
    		vp.addElement(new FloatValue(Sc12));
    		vp.addElement(new FloatValue(Sc12_blw));
    		vp.addElement(new FloatValue(Black));
    		vp.addElement(new FloatValue(Brown));
    		vp.addElement(new FloatValue(FM));
    		vp.addElement(new FloatValue(Broken));
    		vp.addElement(new LongValue(SamDtsId));
    		ret = getSQLCommand().executeUpdate(Sql,vp);	  		
	  		
	  	}
	  	else
	  	{
	  		Sql = "update quality_report " +
  			"SET type = ?, " +
  			"ref_number = ?, " +
  			"date = ?, " +
  			"sc20 = ?, " +
  			"sc19 = ?, " +
  			"sc18 = ?, " +
  			"sc17 = ?, " +
  			"sc16 = ?, " +
  			"sc15 = ?, " +
  			"sc14 = ?, " +
  			"sc13 = ?, " +
			"sc12 = ?, " +
			"below_sc12 = ?, " +
			"black = ?, " +
			"brown = ?, " +
			"foreign_matter = ?, " +
			"broken = ? " +
			"where sample_dts_id = ?";
	  		
    		Vector vp = new Vector();
    		vp.addElement(new StringValue(Type));
    		vp.addElement(new StringValue(QRef));
    		vp.addElement(new DateValue(Utils.today()));
    		vp.addElement(new FloatValue(Sc20_abv));
    		vp.addElement(new FloatValue(Sc19));
    		vp.addElement(new FloatValue(Sc18));
    		vp.addElement(new FloatValue(Sc17));
    		vp.addElement(new FloatValue(Sc16));
    		vp.addElement(new FloatValue(Sc15));
    		vp.addElement(new FloatValue(Sc14));
    		vp.addElement(new FloatValue(Sc13));
    		vp.addElement(new FloatValue(Sc12));
    		vp.addElement(new FloatValue(Sc12_blw));
    		vp.addElement(new FloatValue(Black));
    		vp.addElement(new FloatValue(Brown));
    		vp.addElement(new FloatValue(FM));
    		vp.addElement(new FloatValue(Broken));
    		vp.addElement(new LongValue(SamDtsId));
    		ret = getSQLCommand().executeUpdate(Sql,vp);	  		
	  		
	  		
	  	}
	  	return ret;
	  }
	  	  
}
